import React from 'react';
import { WeaTools, WeaLocaleProvider, WeaTextarea } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import { Button } from 'antd'
import { toJS } from "mobx";
import { each } from 'lodash';

export default class DataCondition extends React.Component {

    render() {
        const { store, ...restProps } = this.props;
        const { fieldProps } = store;
        let value = "";
        if (fieldProps) {
            const cellType = fieldProps.cellType();
            if (cellType) {
                const { conditionInfo } = cellType;
                if (conditionInfo) {
                    const { conditiontype, conditionsql, conditiontext, conditionsql2 } = conditionInfo;
                    if (conditiontype === "1") {
                        value = conditionsql;
                    } else {
                        value = conditionsql2;
                    }
                }
            }

        }
        return (
            <div className="ant-row">
                <label>设置条件</label>
                <Button onClick={() => this.editCondition()} style={{ "border": "0", "float": "right", "margin-top": "-30px", "margin-right": "4px" }}><i className="icon-coms-Flow-setting"></i></Button>
                <WeaTextarea viewAttr={2} value={value} {...restProps} disabled={true} />
            </div>
        )
    }

    editCondition = () => {
        const { store, ...restProps } = this.props;
        const { conditionDialogStore, fieldProps } = store;
        const { range, table, tableOptions } = fieldProps;
        const { sheet } = range;
        let targetValueOptions = [];
        if (sheet) {
            const { data } = sheet.toJSON();
            if (data) {
                const { dataTable } = data;
                if (dataTable) {
                    each(dataTable, (o, rowIndex) => {
                        each(o, (obj, colIndex) => {
                            const bindingPath = sheet.getBindingPath(rowIndex, colIndex);
                            if (bindingPath) {
                                const cellType = sheet.getCell(rowIndex, colIndex).cellType();
                                if (cellType) {
                                    targetValueOptions.push({
                                        key: "$" + bindingPath + "$",
                                        showname: "主表." + cellType.label
                                    });
                                }
                            }
                        });
                    });

                }
            }
        }
        if (table) {
            const { columns, name, bindingPath } = table.toJSON();
            const nameArr = name.split('detail');
            if (nameArr && nameArr[1]) {
                columns.forEach(column => {
                    if (column) {
                        targetValueOptions.push({
                            key: "$" + bindingPath + "." + column.dataField + "$",
                            showname: "明细" + nameArr[1] + "." + column.name
                        });
                    }
                });
            }

        }
        if (fieldProps.cellType()) {
            const cellType = fieldProps.cellType();
            if (cellType) {
                fieldProps.cellTypeId = cellType.id; 
                if (cellType.ds) {
                    conditionDialogStore.initCondition(fieldProps, cellType.ds, targetValueOptions, tableOptions);
                }
            }   
        }
    }

}